package display 
{
	import controller.Controller;
	import events.DisplayEvent;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import utils.DrawingShapes;
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	public class DisplayFeatureList extends MovieClip 
	{
		private var control:Controller;
		
		private var moduleBackGroundColor:uint;
		private var moduleAlpha:Number = 1;
		private var colorOne:uint = 0xcccccc;
		private var colorTwo:uint = 0xffffff;
		private var selectedColorOne:uint = 0x000000;
		private var selectedColorTwo:uint = 0xCCCCCC;
		private var selectedIndex:int = 0;
		private var thumbColorOne:uint = 0x000000;
		private var thumbColorTwo:uint = 0x000000;
		private var ratingStarColor:uint = 0xFFFF00;
		
		private var fontColor:uint = 0x000000;
		private var activeFontColor:uint = 0x000000;
		private var font:String = 'verdana';
		private var textField:DisplayTextField;
		private var displayHighLight:DisplayHighLight;
		
		// sprites
		
		private var mHolder:Sprite;
		private var buttonHolder:Sprite;
		private var thumbHolder:Sprite;
		private var bgSprite:Sprite;
		private var tapButton:Sprite;
		private var star:Sprite;
		
		// movieclips
		
		private var thumbPic:ThumbPicture;
		
		public function DisplayFeatureList(ctr:Controller) 
		{
			control = ctr;
			
			control.addEventListener(DisplayEvent.COLOR_CHANGED, onColorChanged);
			control.addEventListener(DisplayEvent.FONT_CHANGED, onFontChanged);
			control.addEventListener(DisplayEvent.ALPHA_CHANGED, onAlphaChanged);
			control.addEventListener(DisplayEvent.VISIBILITY_CHANGED, onVisibleChanged);
			
			build();
		}
		
		private function build():void
		{
			mHolder = new Sprite;
			mHolder.y = 410;
			
			buttonHolder = new Sprite;
			buttonHolder.y = 380;
			
			thumbHolder = new Sprite;
			thumbHolder.x = 5;
			thumbHolder.y = 10;
			
			addChild(mHolder);
			addChild(buttonHolder);
		}
		
		private function createButton(_colorOne:uint,_colorTwo:uint,_height:int,_width:int,_topLeft:int,_topRight:int,_bottomLeft:int,_bottomRight:int):Sprite
		{
			var matr:Matrix = new Matrix();
			var sprMethod:String = SpreadMethod.PAD;
			
			matr.createGradientBox(_width,_height, Math.PI/2, 0, 0);
			
			tapButton = new Sprite;
			tapButton.graphics.beginGradientFill('linear', [_colorOne, _colorTwo], [1, 1], [0, 255], matr, sprMethod);
			tapButton.graphics.drawRoundRectComplex(0, 0, _width, _height, _topLeft, _topRight, _bottomLeft, _bottomRight);
			
			return tapButton;
		}
		
		private function createButtons():void
		{
			while (buttonHolder.numChildren != 0)
			{
				buttonHolder.removeChildAt(0);
			}
			
			var button:Sprite;
			var buttonGradientHighLight:DisplayHighLight;
			var tapHightLight:DisplayHighLight;
			var tapSelectedHighLight:DisplayHighLight;
			
			for (var i:int = 0; i < 3; i++)
			{
				if (i == selectedIndex)
				{
					button = createButton(selectedColorOne, selectedColorTwo, 30, 118, 8, 8, 0, 0);
					tapSelectedHighLight = new DisplayHighLight( control, 'tapButtonSelectedGradient', 30, 118, 0, 0);
					displayHighLight = new DisplayHighLight( control, 'activeFontColor', 30, 118);
					tapHightLight = new DisplayHighLight( control, 'tapButtonSelectedFontColor', 30, 118, i * 120);
					textField = new DisplayTextField(font, activeFontColor);
				}
				else
				{
					button = createButton(colorOne, colorTwo, 28, 118, 8, 8, 0, 0);
					displayHighLight = new DisplayHighLight( control, 'fontColor', 30, 118, i * 120);
					buttonGradientHighLight = new DisplayHighLight( control, 'buttonGradient', 30, 118, i * 120);
					textField = new DisplayTextField(font, fontColor);
				}
				
				button.x = i * 120;
				
				//textfield
				
				textField.x = 10;
				textField.y = 8;
				
				button.addChild(textField);
				
				buttonHolder.addChild(button);
				buttonHolder.addChild(displayHighLight);
				
				if (buttonGradientHighLight)
				buttonHolder.addChild(buttonGradientHighLight);
				
				if (tapHightLight)
				buttonHolder.addChild(tapHightLight);
				
				if (tapSelectedHighLight)
				buttonHolder.addChild(tapSelectedHighLight);
			}
		}
		
		private function createThumb():void
		{
			var thumbPane:Sprite;
			var stars:Sprite;
			var text:DisplayTextField;
			var displayHighLight:DisplayHighLight;
			var displayHighLightTwo:DisplayHighLight;
			var thumbGradient:DisplayHighLight;
			var thumbSelectedGradient:DisplayHighLight;
			
			for (var x:int = 0; x < 3; x++)
			{
				displayHighLight = new DisplayHighLight( control, 'fontColor', 67, 255);
				displayHighLightTwo = new DisplayHighLight( control,'ratingStarColor', 67, 255);
				thumbGradient = new DisplayHighLight( control,'thumbGradient', 67, 255);
				
				thumbPic = new ThumbPicture;
				thumbPic.x = 5;
				thumbPic.y = 5;
				
				text = new DisplayTextField(font, fontColor,'Bold');
				text.x = 110;
				text.y = 20;
				text.label.text = 'Mats test';
				
				thumbPane = new Sprite;
				thumbPane = createButton(thumbColorOne, thumbColorTwo, 67, 255, 4, 4, 4, 4);
				
				thumbPane.addChild(thumbGradient);
				thumbPane.addChild(displayHighLight);
				thumbPane.addChild(displayHighLightTwo);
				thumbPane.x = x * 260;
				
				stars = createStars();
				stars.x = 120;
				stars.y = 50;
				
				thumbPane.addChild(thumbPic);
				thumbPane.addChild(text);
				thumbPane.addChild(stars);
				
				thumbHolder.addChild(thumbPane);
			}
			
			mHolder.addChild(thumbHolder);
		}
		
		private function createModuleBackground():void
		{
			clear();
			
			var bgHighLight:DisplayHighLight = new DisplayHighLight(control, 'moduleBackgroundColor', 86, 785);
			
			bgSprite = new Sprite;
			bgSprite.graphics.beginFill(moduleBackGroundColor, moduleAlpha)
			bgSprite.graphics.drawRect(0, 0, 785, 86);
			bgSprite.graphics.endFill();
			
			mHolder.addChild(bgSprite);
			mHolder.addChild(bgHighLight);
			
			createThumb();
		}
		
		private function createStars():Sprite
		{
			var sHolder:Sprite = new Sprite;
			var s:Shape;
			
			for (var i:int; i < 5; i++)
			{
				s = new Shape;
				
				s.graphics.beginFill(ratingStarColor, 1);
				s.graphics.lineStyle(1, 0x000000);
				DrawingShapes.drawStar( s.graphics, 0, 0, 5, 3, 6, 16);
				s.graphics.endFill();
				
				s.x = i * 16;
				
				sHolder.addChild(s);
			}
				
			return sHolder;
		}
		
		private function clear():void
		{
			while (mHolder.numChildren != 0)
			{
				mHolder.removeChildAt(0);
			}
		}
		
		// events
		
		private function onColorChanged(e:DisplayEvent):void
		{
			switch(e.data.itemCategory)
			{
				case 'thumbGradient':
				thumbColorOne = e.data.colorOne;
				thumbColorTwo = e.data.colorTwo;
				
				createThumb();
				break;
				
				case 'tapButtonSelectedGradient':
				selectedColorOne = e.data.colorOne;
				selectedColorTwo = e.data.colorTwo;
				
				createButtons();
				break;
				
				case 'buttonGradient':
				colorOne = e.data.colorOne;
				colorTwo = e.data.colorTwo;
				
				createButtons();
				break;
				
				case 'moduleBackgroundColor':
				moduleBackGroundColor = e.data.colorOne;
				
				createModuleBackground();
				break;
				
				case 'ratingStarColor':
				ratingStarColor = e.data.colorOne;
				
				createModuleBackground();
				break;
				
				case 'activeFontColor':
				activeFontColor = e.data.colorOne;
				
				createButtons();
				break;
				
				case 'fontColor':
				fontColor = e.data.colorOne;
				
				createButtons();
				createThumb();
				createModuleBackground();
				break;
			}
		}
		
		private function onAlphaChanged(e:DisplayEvent):void
		{
			switch(e.data.itemCategory)
			{
				case 'moduleBackgroundAlpha':
				moduleAlpha = e.data.alpha / 100;
				
				createModuleBackground();
				break;
			}
		}
		
		private function onFontChanged(e:DisplayEvent):void
		{
			font = e.data.font;
			
			createButtons();
			createThumb();
		}
		
		private function onVisibleChanged(e:DisplayEvent):void
		{
			if (e.data.item == 'FeatureList')
			{
				this.visible = e.data.selected;
			}
		}
	}
}